﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LittleBirdAuction.Core
{
    public static class Tools
    {
        public static void Swap<T>(ref T a, ref T b)
        {
            T c = a;
            a = b;
            b = c;
        }

        public static string StrSearch(string needle, string haystack, bool equalSearch)
        {
            if (equalSearch)
            {
                if (needle.Equals(haystack, StringComparison.OrdinalIgnoreCase))
                {
                    return haystack;
                }
            }
            else
            {
                if (needle.IndexOf(haystack, StringComparison.OrdinalIgnoreCase) > 0)
                {
                    return haystack;
                }
            }

            return string.Empty;
        }

        public static string StrSearch(string needle, string haystack)
        {
            return StrSearch(needle, haystack, false);
        }

        public static bool StrToNumber( string str,  out double number, bool negatives)
        {
            if (double.TryParse(str, out number))
            {
                if (!negatives && number < 0)
                {
                    number = -number;
                }

                return true;
            }

            return false;
        }

        public static bool StrToNumber(string str, out double number)
        {
            return StrToNumber(str, out number, true);
        }

        public static bool StrToNumber(string str, out long number, bool negatives)
        {
            double tempNumber = 0;
            number = 0;

            if (double.TryParse(str, out tempNumber))
            {
                if (!negatives && tempNumber < 0)
                {
                    tempNumber = -tempNumber;
                }

                number = (long)(AuctionConst.NUMBER_ACCURACY * tempNumber);

                return true;
            }

            return false;
        }

        public static bool StrToNumber(string str, out long number)
        {
            return StrToNumber(str, out number, true);
        }

        public static bool StrToNumberList(string str, ref List<long> numbers)
        {
            return StrToNumberList(str, ref numbers, true);
        }

        public static bool StrToNumberList(string str, ref List<long> numbers, bool negatives)
        {
            numbers.Clear();

            string subStr = string.Empty;

            double number = 0;

            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] == '-' && string.IsNullOrEmpty(subStr))
                {
                    subStr += "-";
                }
                else if (str[i] == '.' && !string.IsNullOrEmpty(subStr) && !subStr.Equals("-"))
                {
                    subStr += ".";
                }
                else if (Char.IsDigit(str[i]))
                {
                    subStr += str[i];
                }
                else
                {
                    if (!string.IsNullOrEmpty(subStr) && StrToNumber(subStr, out number, negatives))
                    {
                        long accuracyNumber = (long)(AuctionConst.NUMBER_ACCURACY * number);
                        numbers.Add(accuracyNumber);
                    }

                    subStr = string.Empty;
                }
            }

            return numbers.Count > 0;
        }

        public static uint StrFormatRead( ref uint count, string str, string format,  params object[] args)
        {
            /*
            va_list     args;
            const Char* begin = string;

            assert(format != NULL);
            count = 0;

            // build text from format
            va_start(args, format);
            while(*string && *format)
            {
                if(*format == '%' && *(++format) != '%')
                {
                    switch(*(format++))
                    {
                    case 'u':
                        string = StrToNumber(*((Number*)va_arg(args, Number*)), string);
                        count++;
                        break;
                    case 's':
                        StrReadTo((Char*)va_arg(args, Char*), string, *format);
                        count++;
                        break;
                    }
                }
                else if(*(string++) != *(format++))
                    return 0;
            }
            va_end(args);

            if(*format != 0)
                return 0;

            return string - begin;
             */

            return 0;
        }

        public static void StrNCpy(ref string to, string from, uint limit)
        {
            int size = from.Length > limit ? (int)limit : from.Length;

            to = from.Substring(0, size);
        }

        public static void StrNCpy(ref string to, string from, int limit)
        {
            int size = from.Length > limit ? limit : from.Length;

            to = from.Substring(0, size);
        }

        public static int Min(int a, int b)
        {
            return a > b ? b : a;
        }

        public static uint Min(uint a, uint b)
        {
            return a > b ? b : a;
        }

        public static string RemoveNullTerminator(string input)
        {
            int index = input.IndexOf('\0');
            if (index < 0)
                return input;

            return input.Substring(0, index);
        }


    }
}
